package hu.bme.aut.consulttime.server.entityservices;

import hu.bme.aut.consulttime.client.StudentTeamMembershipService;
import hu.bme.aut.consulttime.entitydtos.StudentTeamMembershipDTO;
import hu.bme.aut.consulttime.entitydtos.TeamDTO;
import hu.bme.aut.consulttime.server.entities.Student;
import hu.bme.aut.consulttime.server.entities.StudentTeamMembership;
import hu.bme.aut.consulttime.server.entities.StudentTeamMembershipId;
import hu.bme.aut.consulttime.server.entities.Team;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.TypedQuery;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

public class StudentTeamMembershipServiceImpl extends RemoteServiceServlet implements StudentTeamMembershipService {

	private static final long serialVersionUID = 6905674004118998487L;

	public static final EntityManager entityManager() {
		return EntityManagerFactoryWrapper.get().createEntityManager();
	}

	@Override
	public List<StudentTeamMembershipDTO> retrieveStudentTeamMemberships(long studentId) {
		
		EntityManager em = entityManager();
		List<StudentTeamMembership> studentTeamMemberships = null;

		try {
			TypedQuery<StudentTeamMembership> query = em.createQuery("select stm from StudentTeamMembership stm where stm.studentTeamMembershipId.student.userId = ?1", StudentTeamMembership.class);
			query.setParameter(1, studentId);
			studentTeamMemberships = query.getResultList();
		} 
		finally {
			em.close();
		}

		List<StudentTeamMembershipDTO> studentTeamMembershipDTOs = new ArrayList<StudentTeamMembershipDTO>(studentTeamMemberships != null ? studentTeamMemberships.size() : 0);
	    if (studentTeamMemberships != null) {
	      for (StudentTeamMembership stm : studentTeamMemberships) {
	    	  StudentTeamMembershipDTO studentTeamMembershipDTO = new StudentTeamMembershipDTO(stm.getTeam().getTeamId(), stm.getStudent().getUserId(), stm.getTeam().getProject().getProjectId(), stm.getTeam().getProject().getTitle(), stm.getTeam().getProject().getType(), stm.getTeam().getName(), stm.getTeam().getSemester().getSemesterId());
	    	  studentTeamMembershipDTOs.add(studentTeamMembershipDTO);
	      }
	    }
		
		return studentTeamMembershipDTOs;
	}
	
	@Override
	public List<StudentTeamMembershipDTO> retrieveStudentTeamMemberships(TeamDTO teamDTO) {
		
		EntityManager em = entityManager();
		List<StudentTeamMembership> studentTeamMemberships = null;

		try {
			TypedQuery<StudentTeamMembership> query = em.createQuery("select stm from StudentTeamMembership stm where stm.studentTeamMembershipId.team.teamId = ?1", StudentTeamMembership.class);
			query.setParameter(1, teamDTO.getTeamId());
			studentTeamMemberships = query.getResultList();
		} 
		finally {
			em.close();
		}

		List<StudentTeamMembershipDTO> studentTeamMembershipDTOs = new ArrayList<StudentTeamMembershipDTO>(studentTeamMemberships != null ? studentTeamMemberships.size() : 0);
	    if (studentTeamMemberships != null) {
	      for (StudentTeamMembership stm : studentTeamMemberships) {
	    	  StudentTeamMembershipDTO studentTeamMembershipDTO = new StudentTeamMembershipDTO(stm.getTeam().getTeamId(), stm.getStudent().getUserId(), stm.getTeam().getProject().getProjectId(), stm.getTeam().getProject().getTitle(), stm.getTeam().getProject().getType(), stm.getTeam().getName(), stm.getTeam().getSemester().getSemesterId());
	    	  studentTeamMembershipDTO.setConsultDay(stm.getConsultDay());
	    	  studentTeamMembershipDTO.setConsultTime(stm.getConsultTime());
	    	  studentTeamMembershipDTOs.add(studentTeamMembershipDTO);
	      }
	    }
		
		return studentTeamMembershipDTOs;
	}

	public Boolean modifyConsultTime(StudentTeamMembershipDTO sgmDTO) {
		EntityManager em = entityManager();
		EntityTransaction tx = em.getTransaction();

		Student student = em.find(Student.class, sgmDTO.getStudentId());
		Team team = em.find(Team.class, sgmDTO.getTeamId());
		if (team != null && student != null) {
			StudentTeamMembership stm = em.find(StudentTeamMembership.class,
					new StudentTeamMembershipId(student, team));

			stm.setConsultDay(sgmDTO.getConsultDay());
			stm.setConsultTime(sgmDTO.getConsultTime());

			try {
				tx.begin();

				em.merge(stm);

				tx.commit();
				return true;
			} catch (Throwable t) {
				t.printStackTrace();
				tx.rollback();
				return false;
			} finally {
				if (em != null) {
					em.close();
				}
			}
		} else {
			return false;
		}
	}

}
